using System;
using System.Collections.Generic;
using System.Text;

using Deepcode.Flux.Core.Systems.CMS;

namespace Deepcode.Flux.Core.UI.CMS
{
	/// <summary>
	/// Base class for all CMS Workflow view forms
	/// </summary>
	public abstract class CMSWFEditForm : Deepcode.Flux.Core.UI.AdministrationModulePage
	{
		#region Permission Code Override
		public override string RequiredPermissionCode { get { return "FluxCMSAccess"; } }
		#endregion
		#region ViewState and Properties
		/// <summary>
		/// Store the ID of the content passed to the viewer
		/// </summary>
		protected int ContentID
		{
			get
			{
				object o = ViewState["CID"];
				if (o == null) return -1;
				return (int)o;
			}
			set
			{
				ViewState["CID"] = value;
			}
		}
		/// <summary>
		/// Store the ID of the taskID passed to the viewer
		/// </summary>
		protected int TaskID
		{
			get
			{
				object o = ViewState["TID"];
				if (o == null) return -1;
				return (int)o;
			}
			set
			{
				ViewState["TID"] = value;
			}
		}
		#endregion
		#region Page Load Event
		protected void Page_Load(object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				// Load path data
				ContentID = GetAttribute("ID", -1);
				TaskID = GetAttribute("TaskID", -1);

				// Check and load the content
				ContentManager c = new ContentManager(true);
				ContentObject o = c.AdxGetByID(ContentID);
				if (o == null) GoBack();
				else
					LoadContent(o);
			}
		}
		#endregion
		#region Utility Methods
		/// <summary>
		/// Return to taskpad
		/// </summary>
		protected virtual void GoBack()
		{
			Response.Redirect("~/admin/menu/taskpad.aspx", true );
		}
		#endregion
		#region Button Handlers
		/// <summary>
		/// Hook this up to the standard update button, it will in turn
		/// load the existing content and invoke the savecontent method
		/// to get field data before updating back to the database.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void btnUpdate_Click(object sender, EventArgs e)
		{
			if (!Page.IsValid) return;

			ContentManager cms = new ContentManager(true);

			// Modification of an existing record
			ContentObject co = cms.AdxGetByID(ContentID);
			if (co == null) GoBack();
			SaveContent(ref co);
			cms.Save(co, CurrentUser.PK_ID);

			// Audit
			Deepcode.Flux.Core.Systems.Audit.Quick.QuickAudit.RaiseInformation(new Deepcode.Flux.Core.Systems.Audit.Quick.AuditUser(),
				"Flux",
				"Content Manager",
				"modified content during workflow {0}", co.NodeTitle);

		}
		#endregion
		#region Virtual methods and properties
		/// <summary>
		/// Override and implement this method to populate
		/// the form from the content object specified
		/// </summary>
		/// <param name="load"></param>
		protected virtual void LoadContent(ContentObject load)
		{
		}
		/// <summary>
		/// Override and implement this method to store
		/// fields back to the content object specified
		/// </summary>
		/// <param name="save"></param>
		protected virtual void SaveContent(ref ContentObject save)
		{
		}
		#endregion
	}
}
